/**
 * 
 */
package pl.edu.agh.to;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

/**
 * @author karulis
 * 
 */
public class ScheduleActivity extends TemplateActivity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	int getLayoutResID() {
		return R.layout.schedule;
	}

	@Override
	int getSpinnerID() {
		return R.id.schedule_spinner;
	}

	@Override
	int getListViewID() {
		return R.id.schedule_list;
	}

	@Override
	protected void scheduleChange() {
		setupList();
	}

	@Override
	List<Object> listViewContent() {
		return getdBAccess().getScheduleFor(getSpinnerSelectedItem());
	}

	private List<Object> toObjectList(List<? extends Object> list) {
		List<Object> strList = new ArrayList<Object>();
		for (Object obj : list) {
			strList.add(obj);
		}
		return strList;
	}

	@Override
	List<Object> spinnerContent() {
		return toObjectList(Arrays.asList(getResources().getStringArray(
				R.array.days_array)));
	}

	@Override
	protected int getMenuRes() {
		return R.menu.schedule_menu;
	}

	@Override
	protected void attendChange() {
		// do nothing
	}

	@Override
	protected void rateChange() {
		// do nothing
	}

	@Override
	protected void courseChange() {
		setupList();
	}

	@Override
	protected void participantChange() {
		// do nothing
	}

}

class ScheduleAddDialog extends AddToDialog implements OnItemSelectedListener {

	ScheduleAddDialog(TemplateActivity activity) {
		super(activity);
	}

	@Override
	public void prepare() {
		super.prepare();
		ArrayAdapter<Object> adapter = new ArrayAdapter<Object>(getActivity(),
				android.R.layout.simple_spinner_item, getSpinnerContent());
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		getDialogSpinner().setAdapter(adapter);
		getDialogSpinner().setOnItemSelectedListener(this);

		ArrayAdapter<Object> adapter1 = new ArrayAdapter<Object>(getActivity(),
				android.R.layout.simple_spinner_item, getStartSpinnerContent());
		adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		getDialogStartSpinner().setAdapter(adapter1);
		getDialogStartSpinner().setOnItemSelectedListener(this);

		ArrayAdapter<Object> adapter2 = new ArrayAdapter<Object>(getActivity(),
				android.R.layout.simple_spinner_item, getStartSpinnerContent());
		adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		getDialogStopSpinner().setAdapter(adapter2);
		getDialogStopSpinner().setOnItemSelectedListener(this);

	}

	/**
	 * @return
	 */
	@Override
	protected int getViewId() {
		return R.layout.schedule_add_dialog;
	}

	/**
	 * @return
	 */
	@Override
	protected List<Object> getSpinnerContent() {
		return getdBAccess().getAllCourses();
	}

	protected List<Object> getStartSpinnerContent() {
		List<Object> times = new ArrayList<Object>();
		NumberFormat format = new DecimalFormat("00");
		for (int i = 7; i < 21; i++) {
			for (int j = 0; j < 60; j += 15) {
				times.add(format.format(Integer.parseInt(String.valueOf(i)))
						+ ":"
						+ format.format(Integer.parseInt(String.valueOf(j))));
			}
		}
		return times;
	}

	protected List<Object> getStopSpinnerContent() {
		return getStartSpinnerContent();
	}

	@Override
	protected int getMessageId() {
		return R.string.schedule_add_dialog_text;
	}

	/**
	 * @return
	 */
	@Override
	protected int getDialogSpinnerId() {
		return R.id.schedule_courses_spinner;
	}

	protected int getDialogStartSpinnerId() {
		return R.id.schedule_start_time_spinner;
	}

	Spinner getDialogStartSpinner() {
		return (Spinner) findViewById(getDialogStartSpinnerId());
	}

	protected int getDialogStopSpinnerId() {
		return R.id.schedule_stop_time_spinner;
	}

	Spinner getDialogStopSpinner() {
		return (Spinner) findViewById(getDialogStopSpinnerId());
	}

	@Override
	protected void action() {
		Course course = getdBAccess().getCourse(getDialogSpinnerItem());
		String start = getDialogStartSpinner().getSelectedItem().toString();
		String stop = getDialogStopSpinner().getSelectedItem().toString();
		String day = getSpinnerItem();
		getdBAccess().addSchedule(new ScheduleItem(day, start, stop, course));
	}

	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		// TODO add spinner content update and

	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub

	}
}

class RemoveSchedule extends AddToDialog {

	RemoveSchedule(TemplateActivity activity) {
		super(activity);
	}

	@Override
	protected int getViewId() {
		return R.layout.remove_schedule_dialog;
	}

	@Override
	protected List<Object> getSpinnerContent() {
		return getdBAccess().getScheduleFor(getSpinnerItem());
	}

	@Override
	protected int getDialogSpinnerId() {
		return R.id.remove_schedule_spinner;
	}

	@Override
	protected int getMessageId() {
		return R.string.schedule_dialog_text;
	}

	@Override
	protected void action() {
		String day = getSpinnerItem();
		String item = getDialogSpinnerItem();
		if (day != null && item != null) {
			String[] data = getDialogSpinnerItem().split("\\s+");
			getdBAccess().removeSchedule(
					new ScheduleItem(day, data[0], data[2], new Course(data[3],
							data[5])));
		}
	}

}